from django.core.paginator import Paginator
from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse
from django.views.generic import ListView, CreateView,DetailView

from post.models import ResumePost, PositionPost
from django.shortcuts import redirect, render

# def home(request):
#     return HttpResponse("我是首页")


# 简历页视图==================================================
class ResumeDetail(ListView):
    template_name = 'resume.html'
    model = ResumePost
    context_object_name = "re"
    paginate_by = 1

    # 增加简历===============================================
class ResumeAdd(CreateView):
    def post(self,request,*args,**kwargs):
        user_id=request.POST.get("user_id")
        username=request.POST.get("username")
        sex=request.POST.get("sex")
        edu=request.POST.get("edu")
        mobile=request.POST.get("mobile")
        email=request.POST.get("email")
        position=request.POST.get("position")
        exper=request.POST.get("exper")
        description=request.POST.get("description")
        id=request.POST.get("id")
        ResumePost.objects.create(user_id=user_id,id=id,username=username,sex=sex,edu=edu,mobile=mobile,email=email,position=position,exper=exper,description=description)
        return redirect('/')
# 简历显示功能==========================================================================
class ResumeDetaill(DetailView) :
    template_name = "resume.html"
    model = ResumePost
    context_object_name = "res"
    def get_context_data(self, **kwargs):
        pk = self.kwargs.get(self.pk_url_kwarg, None)
        kwargs["comment_list"] = ResumePost.objects. \
            filter(user_id=pk)
        return super().get_context_data(**kwargs)


#工作岗位展示=================================================
class PositListView(ListView):
    template_name = "position.html"
    model = PositionPost
    context_object_name = "post"
    paginate_by = 10

# 工作岗位查询===========================================================
def searchform(request):

    q = request.GET.get('q')
    error_msg = ''

    if not q:
        error_msg = '请输入关键词'
        return render(request, 'post.html', {'error_msg': error_msg})

    post_list = PositionPost.objects.filter(p_name__contains=q)
    return render(request, 'post.html', {'error_msg': error_msg,
                                               'post_list': post_list})




# 工作岗位详情页展示==========================================================

def detail(request,pk):
    job=PositionPost.objects.get(pk=pk)
    return render(request,'detail.html',context={"job":job})



# 数据可视化——=============================================================================
from django.db.models import Count,F
def echart(request):
    result = PositionPost.objects.values("p_edu").annotate(value=Count("id"),name=F("p_edu"))
    result = list(result)
    return render(request,"echart.html",{"name_data":result})

# 分页======================================================================================================================

def article_list(request):
    # return HttpResponse("Hello World!")
    q = request.GET.get('q')
    article_list = PositionPost.objects.filter(p_name__contains=q)
    # 修改变量名称（articles -> article_list）

    # 每页显示 3 篇文章
    paginator = Paginator(article_list, 3)
    # 获取 url 中的页码
    page = request.GET.get('page')
    # 将导航对象相应的页码内容返回给 articles
    articles = paginator.get_page(page)

    context = {'articles': articles}
    # render函数：载入模板，并返回context对象
    return render(request, 'position.html', context)


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def score_dis(request):
    q = request.GET.get('q')
    all_scores = PositionPost.objects.filter(p_name__contains=q)
    page = request.GET.get("page", 1)

    paginator = Paginator(all_scores, 20)

    try:
        all_scores = paginator.page(page)
    except PageNotAnInteger:
        all_scores = paginator.page(1)
    except EmptyPage:
        all_scores = paginator.page(paginator.num_pages)

    return render(request, "position.html", {"all_scores": all_scores})


# ======================================================
# 简历投递






